/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.transformer.basic.service.impl;

import me.zhengjie.modules.transformer.basic.domain.TransformerBasic;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.transformer.basic.repository.TransformerBasicRepository;
import me.zhengjie.modules.transformer.basic.service.TransformerBasicService;
import me.zhengjie.modules.transformer.basic.service.dto.TransformerBasicDto;
import me.zhengjie.modules.transformer.basic.service.dto.TransformerBasicQueryCriteria;
import me.zhengjie.modules.transformer.basic.service.mapstruct.TransformerBasicMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 陈佳豪
 * @website https://el-admin.vip
 * @description 服务实现
 * @date 2021-12-01
 **/
@Service
@RequiredArgsConstructor
public class TransformerBasicServiceImpl implements TransformerBasicService {

    private final TransformerBasicRepository transformerBasicRepository;
    private final TransformerBasicMapper transformerBasicMapper;

    @Override
    public Map<String, Object> queryAll(TransformerBasicQueryCriteria criteria, Pageable pageable) {
        Page<TransformerBasic> page = transformerBasicRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(transformerBasicMapper::toDto));
    }

    @Override
    public Set querySubstation() {
        List<TransformerBasic> bySubstationName = transformerBasicRepository.findAll();
        Set substations = new HashSet();
        int i = 0;
        for (TransformerBasic tb : bySubstationName) {
            substations.add(tb.getSubstationName());
        }
        return substations;
    }

    @Override
    public Set queryRunNumbersBySubstation(String subname) {
        List<TransformerBasic> bySubstationName = transformerBasicRepository.findBySubstationName(subname);
        Set runNumbers = new HashSet();
        for (TransformerBasic tb : bySubstationName) {
            runNumbers.add(tb.getRunNumber());
        }
        return runNumbers;
    }

    @Override
    public List<TransformerBasicDto> queryAll(TransformerBasicQueryCriteria criteria) {
        return transformerBasicMapper.toDto(transformerBasicRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public TransformerBasicDto findById(Integer id) {
        TransformerBasic transformerBasic = transformerBasicRepository.findById(id).orElseGet(TransformerBasic::new);
        ValidationUtil.isNull(transformerBasic.getId(), "TransformerBasic", "id", id);
        return transformerBasicMapper.toDto(transformerBasic);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TransformerBasicDto create(TransformerBasic resources) {
        return transformerBasicMapper.toDto(transformerBasicRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TransformerBasic resources) {
        TransformerBasic transformerBasic = transformerBasicRepository.findById(resources.getId()).orElseGet(TransformerBasic::new);
        ValidationUtil.isNull(transformerBasic.getId(), "TransformerBasic", "id", resources.getId());
        transformerBasic.copy(resources);
        transformerBasicRepository.save(transformerBasic);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            transformerBasicRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TransformerBasicDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TransformerBasicDto transformerBasic : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put(" createTime", transformerBasic.getCreateTime());
            map.put(" updateTime", transformerBasic.getUpdateTime());
            map.put(" createBy", transformerBasic.getCreateBy());
            map.put(" updateBy", transformerBasic.getUpdateBy());
            map.put("设备型号", transformerBasic.getEquipmentModel());
            map.put("变电站名称", transformerBasic.getSubstationName());
            map.put("生产厂家", transformerBasic.getManufacturer());
            map.put("生产日期", transformerBasic.getDateOfManufacture());
            map.put("投运日期", transformerBasic.getDateOfPut());
            map.put("电压等级", transformerBasic.getVoltageClasses());
            map.put("变压器运行编号", transformerBasic.getRunNumber());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}